using System;
using System.Collections.Generic;
using System.Text;
using System.Web ;


namespace eBook.Services.Amazon
{
  public class SearchCriteria
  {
    private string _keywords;
    public string Keywords
    {
      get { return _keywords; }
      set { _keywords = value ?? string.Empty; }
    }

    private string _language = string.Empty; 
    public string Language
    {
      get { return _language; }
      set { _language = value ?? string.Empty; }
    }

    private string _ISBN = string.Empty;
    public string ISBN
    {
      get { return _ISBN; }
      set { _ISBN = value ?? string.Empty; }
    }

    private string _author = string.Empty;
    public string Author
    {
        get { return _author; }
        set { _author = value == null ? _author = string.Empty :_author = value; }
    }

    private string _title = string.Empty;
    public string Title
    {
      get { return _title; }
      set { _title = value ?? string.Empty; }
    }

    private string _powerSearchText = string.Empty;
    public string PowerSearchText
    {
      get 
      {
        // http://www.amazon.com/exec/obidos/ats-query-page#powersearch
        string pText = string.Empty;
        
        if (!string.IsNullOrEmpty(_ISBN))
          pText += string.Format(" {0} isbn:{1}", (pText.Length > 0 ? "and" : string.Empty), _ISBN);        
        
        if (!string.IsNullOrEmpty(_language))  
        pText += string.Format(" {0} language:{1}", (pText.Length > 0 ? "and" : string.Empty), _language);
        
        //pText += (_title != "" && pText.Length > 0 ? " and " : "") + "title:" + _title;
        //pText += (_author != "" && pText.Length > 0 ? " and " : "") + "author:" + _author;             
        //return MyUrlEncode(pText);
        return pText;
      }
      set { _powerSearchText = value; }
    }

    public SearchCriteria()
    {
      this.Reset();
    }

    public void Reset()
    {
      _ISBN = "";
      _title = "";
      _author = "";
      _language = "";
      _keywords = "";
    }

    //public string MyUrlEncode(string str)
    //{
    //  StringBuilder sb = new StringBuilder();

    //  foreach (char c in str)
    //    if (char.IsLetterOrDigit(c))
    //      sb.Append(c);
    //    else
    //      sb.AppendFormat("%{0:X02}", (int)c);

    //  return sb.ToString();
    //}
  }
}
